<?php
/**
 * CEOfox
 *
 * @copyright  Copyright 2011-2012 CEOfox, Inc.
 * @license    http://ceofox.com/pages/license
 * @version    $Id: pages.class.php 47083 2013-05-24 09:40:17 $
 * @author     CEOfox, Inc.
 */
?>
<?php
  defined('PHPFOX') or exit('NO DICE!');
  class Follow_Component_Controller_Pages extends Phpfox_Component
  {
        public function process()
        {
            $sView = $this->request()->get('req4', 'follower');
            if($aParentModule = $this->getParam('aParentModule'))
            {      
                if($aParentModule['module_id'] != 'pages')
                {
                    Phpfox_Error::set(Phpfox::getPhrase('follow.page_not_found'));
                    return false;
                }
            }
            
            if($sView != 'setting')
            {
                $this->search()->set(array(
                    'type' => 'follow',
                    'field' => 'follow.user_id',                
                    'search_tool' => array(
                        'table_alias' => 'follow',
                        'search' => array(
                            'action' => '',
                            'default_value' => Phpfox::getPhrase('follow.search_follow_dot'),
                            'name' => 'search',
                            'field' => array('follow.user_id', 'follow.item_id')
                        ),
                        'sort' => array(
                            'latest' => array('follow.time_stamp', Phpfox::getPhrase('follow.lastest')),
                            'full_name' => array('follow.full_name', Phpfox::getPhrase('user.full_name')),
                        ),
                        'show' => array(5, 10, 15),
                    )
                ));
                
                $aBrowseParams = array(
                    'module_id' => 'follow',
                    'alias' => 'follow',
                    'field' => 'user_id',
                    'table' => Phpfox::getT('follow')
                );
                
                $sKeyword = '';
                $aItems = array();
                $iCnt = 0;
                if($sKeyword = $this->search()->get('search'))
                {
                    $iAmount = ($this->request()->getInt('show'))? $this->request()->get('show'): Phpfox::getParam('follow.showeachtime');
                    $iOffset = ($this->request()->getInt('page') >= 2)?($this->request()->getInt('page') - 1) * $iAmount : 0;
                    switch($sView)
                    {
                        case "follower":
                        
                            list($iCnt, $aItems) = Phpfox::getService('follow')->getFollower($aParentModule['item_id'], $iOffset, Phpfox::getParam('follow.showeachtime'), $aParentModule['module_id'], $sKeyword);
                            if($aItems)
                            {
                                foreach($aItems as $iKey => $aFollower)
                                {
                                    if($aFollower['user_id'] == phpfox::getUserId())
                                    {
                                        $aItems[$iKey]['canFollow'] = 0;
                                        $aItems[$iKey]['isFollow'] = 0;
                                    }
                                    else
                                    {
                                        if(Phpfox::getService('follow')->canFollow($aFollower['user_id']))
                                        {
                                            if(phpfox::getService('follow')->isFollow($aFollower['user_id']) == 0)
                                            {
                                                $aItems[$iKey]['isFollow'] = 0;
                                            }
                                            else
                                            {
                                                $aItems[$iKey]['isFollow'] = 1;
                                            }
                                            $aItems[$iKey]['canFollow'] = 1;
                                        }
                                        else
                                        {
                                            $aItems[$iKey]['isFollow'] = 0;
                                            $aItems[$iKey]['canFollow'] = 0;
                                        }
                                    }
                                }
                            }
                            break;
                        case "following":
                            $aItems = phpfox::getService('follow')->searchFollowing($sKeyword, $aParentModule['item_id'], $iOffset, $iAmount, $aParentModule['module_id']);
                            if($aItems)
                            {
                                foreach($aItems as $iKey => $aFollowing)
                                {
                                    if($aFollowing['user_id'] == phpfox::getUserId())
                                    {
                                        $aItems[$iKey]['canFollow'] = 0;
                                        $aItems[$iKey]['isFollow'] = 0;
                                    }
                                    else
                                    {
                                        if(Phpfox::getService('follow')->canFollow($aFollowing['user_id']))
                                        {
                                            if(phpfox::getService('follow')->isFollow($aFollowing['user_id']) == 0)
                                            {
                                                $aItems[$iKey]['isFollow'] = 0;
                                            }
                                            else
                                            {
                                                $aItems[$iKey]['isFollow'] = 1;
                                            }
                                            $aItems[$iKey]['canFollow'] = 1;
                                        }
                                        else
                                        {
                                            $aItems[$iKey]['isFollow'] = 0;
                                            $aItems[$iKey]['canFollow'] = 0;
                                        }
                                    }
                                }
                            }
                            $iCnt = phpfox::getService('follow')->countsearchFollowing($sKeyword, $aParentModule['item_id'], $aParentModule['module_id']);
                            break;
                        case "blocker":
                            if(Phpfox::getService('pages')->isAdmin($aParentModule['item_id']))
                            {
                                list($iCnt, $aItems) = Phpfox::getService('follow')->getBlocker($aParentModule['item_id'], $iOffset, Phpfox::getParam('follow.showeachtime'), $aParentModule['module_id'], $sKeyword);
                                break;
                            }
                        case "pending":
                            list($iCnt, $aItems) = Phpfox::getService('follow')->getPending($sKeyword, $aParentModule['item_id'], $iOffset, Phpfox::getParam('follow.showeachtime'), $aParentModule['module_id']);
                            break;
                    }
                }
                
                $bIsAdmin = Phpfox::getService('pages')->isAdmin($aParentModule['item_id']);
                $this->setParam('sBlockType', $sView);
                $this->setParam('sKeyword', $sKeyword);
                $this->setParam('iCnt', $iCnt);
                $this->setParam('aItems', $aItems);
            }
            
            $bHasFollow = Phpfox::getService('follow')->isFollow(Phpfox::getUserId(), $aParentModule['item_id'], $aParentModule['module_id']);
            
            $this->template()->setHeader(array(
                'jquery/ui.js' => 'static_script',
            ));
            
            $this->template()->assign(array(
                'aParentModule' => $aParentModule,
                'sView' => $sView,
            ))
                ->setHeader(array(
                    'jquery/plugin/jquery.highlightFade.js' => 'static_script',
                    'jquery/plugin/jquery.scrollTo.js' => 'static_script',
                    'quick_edit.js' => 'static_script',
                    'privacy.css' => 'module_user',
                    'comment.css' => 'style_css',
                    'pager.css' => 'style_css',
                    'feed.js' => 'module_follow',
                    'friend.js' => 'module_friend',
                    'friend.css' => 'style_css',
                    'follow.css' => 'module_follow',
            ))
            ;
            $this->template()->setPhrase(array(
                'follow.following','follow.unfollow','follow.follow'
            ));
        }
        
        public function clean()
        {
            (($sPlugin = Phpfox_Plugin::get('follow.component_controller_pages_clean')) ? eval($sPlugin) : false);
        }
  }
?>
